package pe.gob.sedapal.scmultimedia.rcp.view;

import java.util.Dictionary;
import java.util.Hashtable;

import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IViewReference;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventConstants;
import org.osgi.service.event.EventHandler;
import org.eclipse.swt.SWT;

import pe.gob.sedapal.scmultimedia.rcp.view.centrosservicios.ConsultaCentroServiciosView;
import pe.gob.sedapal.scmultimedia.rcp.view.estadocuenta.ConsultaEstadoCuentaView;
import pe.gob.sedapal.scmultimedia.rcp.view.servicios.ConsultaServiciosView;
import pe.gob.sedapal.scmultimedia.rcp.view.servicios.PdfView;
import pe.gob.sedapal.scmultimedia.util.Constante;
import pe.gob.sedapal.scmultimedia.wb.swt.ResourceManager;
import pe.gob.sedapal.scmultimedia.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseAdapter;

public class NavegacionView extends ViewPart {
	
	public static final String ID = "scmultimedia.navegacionPruebaView";
	@SuppressWarnings("unused")
	private String vistaAnterior;
	private String vistaActual;

	public NavegacionView() {
		vistaAnterior = PrincipalView.ID;
		vistaActual = PrincipalView.ID;
	}

	@Override
	public void createPartControl(final Composite parent) {

		parent.setLayout(new FillLayout());
		parent.setBackground(SWTResourceManager.getColor(173, 216, 230));
		parent.setBackgroundMode(SWT.INHERIT_DEFAULT);
	
		//BOTON INICIO
		final Label btnInicio = new Label(parent, SWT.NONE);
		btnInicio.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				IWorkbenchPage pagina = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
				@SuppressWarnings("deprecation")
				IViewPart[] vistasActuales = pagina.getViews();
				
				for (IViewPart iViewPart : vistasActuales) {
					pagina.hideView(iViewPart);
				}

				try{
					pagina.showView(PrincipalView.ID);
				
				}catch(Exception ex){
					ex.printStackTrace();
				}
			}
		});
		btnInicio.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent e) {
				btnInicio.setImage(ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_IMG_BOTON_INFERIOR_HOME));
			}
			@Override
			public void mouseExit(MouseEvent e) {
			    btnInicio.setImage(ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_IMG_BOTON_INFERIOR_HOME_OFF));
			}
		});
		btnInicio.setImage(ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_IMG_BOTON_INFERIOR_HOME));

		//BOTON ESTADO DE CUENTA
		final Label btnEstCuenta = new Label(parent, SWT.NONE);
		btnEstCuenta.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				IWorkbenchPage pagina = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
				@SuppressWarnings("deprecation")
				IViewPart[] vistasActuales = pagina.getViews();
				
				for (IViewPart iViewPart : vistasActuales) {
					pagina.hideView(iViewPart);
				}
				try{
					pagina.showView(ConsultaEstadoCuentaView.ID);
					pagina.showView(NavegacionView.ID);
					vistaActual = ConsultaEstadoCuentaView.ID;
				}catch(Exception ex){
					ex.printStackTrace();
				}
			}
		});
		btnEstCuenta.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent e) {
				btnEstCuenta.setImage(ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_IMG_BOTON_INFERIOR_SALDO));
			}
			@Override
			public void mouseExit(MouseEvent e) {
				btnEstCuenta.setImage(ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_IMG_BOTON_INFERIOR_SALDO_OFF));
			}
		});	
		btnEstCuenta.setImage(ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_IMG_BOTON_INFERIOR_SALDO));

		//BOTON CENTROS DE SERVICIOS
		final Label btnCentrosServ = new Label(parent, SWT.NONE);
		btnCentrosServ.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				IWorkbenchPage pagina = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
				@SuppressWarnings("deprecation")
				IViewPart[] vistasActuales = pagina.getViews();
				
				for (IViewPart iViewPart : vistasActuales) {
					pagina.hideView(iViewPart);
				}

				try{
					pagina.showView(ConsultaCentroServiciosView.ID);
					pagina.showView(NavegacionView.ID);
					vistaActual = ConsultaEstadoCuentaView.ID;
				}catch(Exception ex){
					ex.printStackTrace();
				}
			}
		});

		btnCentrosServ.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent e) {
				btnCentrosServ.setImage(ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_IMG_BOTON_INFERIOR_OFICINA));
			}
			@Override
			public void mouseExit(MouseEvent e) {
				btnCentrosServ.setImage(ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_IMG_BOTON_INFERIOR_OFICINA_OFF));
			}
		});	
		btnCentrosServ.setImage(ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_IMG_BOTON_INFERIOR_OFICINA));

		//BOTON DE SERVICIOS
		final Label btnServicios = new Label(parent, SWT.NONE);
		btnServicios.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				
				IWorkbenchPage pagina = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
				@SuppressWarnings("deprecation")
				IViewPart[] vistasActuales = pagina.getViews();
				
				for (IViewPart iViewPart : vistasActuales) {
					pagina.hideView(iViewPart);
				}
				try{
					pagina.showView(ConsultaServiciosView.ID);
					pagina.showView(NavegacionView.ID);
					IViewReference vistaPDF  = pagina.findViewReference(PdfView.ID);
					pagina.hideView(vistaPDF);			
					vistaAnterior = vistaActual;
					vistaActual = ConsultaServiciosView.ID;
				}catch(Exception ex){
					ex.printStackTrace();
				}
			}
		});
		btnServicios.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent e) {
				btnServicios.setImage(ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_IMG_BOTON_INFERIOR_SERVICIO));
			}
			@Override
			public void mouseExit(MouseEvent e) {
				btnServicios.setImage(ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_IMG_BOTON_INFERIOR_SERVICIO_OFF));
			}
		});	
		btnServicios.setImage(ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_IMG_BOTON_INFERIOR_SERVICIO));

		BundleContext ctx = FrameworkUtil.getBundle(NavegacionView.class).getBundleContext();
		EventHandler handler = new EventHandler() {
			public void handleEvent(final Event event) {
				if( parent.getDisplay().getThread() == Thread.currentThread() ) {

				} else {
					parent.getDisplay().syncExec(new Runnable() {
						public void run() {

						}
					});
				}
			}
		};
		Dictionary<String,String> properties = new Hashtable<String, String>();
		properties.put(EventConstants.EVENT_TOPIC, "viewcommunication/*");
		ctx.registerService(EventHandler.class, handler, properties); 
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

}
